package academic.ui.client.widgets;

import java.util.List;

import ui.client.GenericContainerTag;

import academic.ui.client.util.ClientHelper;
import ecommerce.soco.shared.GWT_Purchase;
import ecommerce.soco.shared.GWT_Product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;

public class DealsReportPage extends Composite {

	private GenericContainerTag div = new GenericContainerTag("div");
	public DealsReportPage() {
		div.setStyleName("dealsReportPage");
		initWidget(div);
	}
	
	FlexTable dealsReportTable = new FlexTable();
	
	public void setup(List<GWT_Purchase> deals) {
		div.clear();
		dealsReportTable.clear();
		dealsReportTable.setWidth("100%");
		dealsReportTable.setWidget(0, 1, new GenericContainerTag("span","The Deal"));
		dealsReportTable.setWidget(0, 2, new GenericContainerTag("span","Price"));
		dealsReportTable.setWidget(0, 3, new GenericContainerTag("span","Quantity"));
		dealsReportTable.setWidget(0, 4, new GenericContainerTag("span","Total"));
		dealsReportTable.setWidget(0, 5,  new GenericContainerTag("span","Date Purchased"));
		
		for(int i=0;i<deals.size();i++)
		{
			GWT_Purchase deal = deals.get(i);
			GWT_Product product = deal.getProduct();
			Image img = ClientHelper.createImage(GWT.getModuleBaseURL()+"../PICS/Products/"+product.getImageSmall(),product.getSmallImgRatio(),100);
		
			dealsReportTable.setWidget(i+1, 0, img);
			dealsReportTable.setWidget(i+1, 1, new GenericContainerTag("span",product.getName()));
			dealsReportTable.setWidget(i+1, 2, new GenericContainerTag("span",ClientHelper.formatPrice(product.getPrice0())));
			dealsReportTable.setWidget(i+1, 3, new GenericContainerTag("span",String.valueOf(product.getQuantity())));
			dealsReportTable.setWidget(i+1, 4, new GenericContainerTag("span",getTotal(product.getQuantity(),product.getPrice0())));
			dealsReportTable.setWidget(i+1, 5, new GenericContainerTag("span",ClientHelper.formatDate(deal.getPurchaseDate())));
		}
		
		
		div.add(dealsReportTable);
	}
	
	private String getTotal(Integer quantity,Double price) {
		return ClientHelper.formatPrice(quantity*price);
	}
	
}
